package com.yunche.yunchebusiness.ui.activity.brand

import android.content.Intent
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.alibaba.fastjson.JSONObject
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.yunche.yunchebusiness.R
import com.yunche.yunchebusiness.base.BaseActivity
import com.yunche.yunchebusiness.common.Constant
import com.yunche.yunchebusiness.entity.BrandInfoEntity
import com.yunche.yunchebusiness.entity.ModelEntity
import com.yunche.yunchebusiness.entity.SeriesEntity
import com.yunche.yunchebusiness.mvp.contract.SeriesContract
import com.yunche.yunchebusiness.mvp.presenter.SeriesPresenter
import com.yunche.yunchebusiness.network.RequestJson
import com.yunche.yunchebusiness.utils.ToastyUtils
import kotlinx.android.synthetic.main.activity_model.*

class ModelActivity : BaseActivity(), SeriesContract.View, BaseQuickAdapter.OnItemClickListener {

    private val mPresenter: SeriesPresenter by lazy {
        SeriesPresenter()
    }
    var carInfoEntity: BrandInfoEntity? = null
    var mAdapter: ModelAdapter? = null


    override fun getSeriesSuccess(seriesEntity: List<SeriesEntity>) {
    }

    override fun getModelSuccess(modelEntity: List<ModelEntity>) {

        mAdapter = ModelAdapter(R.layout.item_series, modelEntity)
        (mAdapter as ModelAdapter).onItemClickListener = this
        (mAdapter as ModelAdapter).isFirstOnly(false)
        (mAdapter as ModelAdapter).openLoadAnimation(BaseQuickAdapter.SCALEIN)
        rv_model.adapter = mAdapter
    }

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>?, view: View?, position: Int) {

        var entity = adapter!!.data[position] as ModelEntity
        var intent = Intent()
        carInfoEntity?.modelYear = "${entity.modelYear}"
        carInfoEntity?.modelName = entity.name
        carInfoEntity?.modelCode = entity.code
        carInfoEntity?.modelId = "${entity.id}"
        intent.putExtra(Constant.BRAND_INFO_ENTITY, carInfoEntity)
        setResult(Constant.SELECT_MODEL_RESULT_CODE, intent)
        finish()

    }

    override fun showLoading() {
        srflay_model.isRefreshing = true
    }

    override fun hideLoading() {
        srflay_model.isRefreshing = false
    }

    override fun showError(msg: String) {
        ToastyUtils.showError(msg)
    }

    override fun setLayout(): Int = R.layout.activity_model

    override fun initView() {
        mPresenter.attachView(this)
        carInfoEntity = intent.getSerializableExtra(Constant.BRAND_INFO_ENTITY) as BrandInfoEntity
        rv_model.layoutManager = LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false)

    }

    override fun initData() {
        var parmar = JSONObject()
        parmar["series"] = carInfoEntity?.seriesCode
        mPresenter.getModel(RequestJson.getRequestBody(parmar.toJSONString()))
    }

    override fun initEvent() {
        srflay_model.setOnRefreshListener {
            initData()
        }
    }

    inner class ModelAdapter constructor(id: Int, data: List<ModelEntity>) :
        BaseQuickAdapter<ModelEntity, BaseViewHolder>(id, data) {

        override fun convert(helper: BaseViewHolder?, item: ModelEntity?) {
            helper?.setText(R.id.tv_series_name, item!!.name)
            Glide.with(mContext!!).load(carInfoEntity?.seriesIcon).into(helper?.getView(R.id.iv_series_cover)!!)
        }

    }

    override fun onDestroy() {
        super.onDestroy()
        mPresenter.detachView()
    }
}
